package controle;

import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import modelo.Exercicio;
import modelo.Usuario;
import persistencia.ExercicioDAO;
import persistencia.UsuarioDAO;

public class ServletExercicio extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String acao = request.getParameter("acao");

        if (acao.equals("salvar")) {
            
            salvar(request, response);

        } else if (acao.equals("listar")) {

            Recupera(request, response);

        } else if (acao.equals("excluir")) {

            excluir(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    protected void salvar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        String ano = request.getParameter("ano");
        String mes = request.getParameter("mes");
        
        int ano_int = Integer.parseInt(ano);
        int mes_int = Integer.parseInt(mes);
        Usuario usuario = (Usuario) session.getAttribute("usuario");
        
        Exercicio exercicio = new Exercicio(usuario, ano_int, mes_int);
        
        String msg = "";
        if (ExercicioDAO.salvar(exercicio)){
                System.out.print("Servlet: cadastrado com sucess");
                 msg = "Exercicio cadastrado com sucesso";
        }else{
                msg = "Erro ao cadastrar exercicio";
                System.out.print("Servlet: erro ao cadastrar receita");
        }
        
        request.setAttribute("msg", msg);
        getServletContext().getRequestDispatcher("/CadastrarExercicio.jsp").forward(request, response);
    }
    
     protected void Recupera(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        
        try {
            ExercicioDAO a = new ExercicioDAO();
            Vector exercicio = a.getExercicio();
            session.setAttribute("exercicio", exercicio.iterator());
            session.setAttribute("exercicio", exercicio.iterator());
            
            getServletContext().getRequestDispatcher("/Servlete?acao=listar").forward(request, response);
          }catch(Exception e) {
            System.out.println("Erro n servlet:");
            e.printStackTrace();
            out.close();
        }
    }
    
      protected void excluir(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        int id_exercicio = Integer.parseInt(request.getParameter("id_exercicio"));
        ExercicioDAO dao = new ExercicioDAO();
        dao.excluir(id_exercicio);
        getServletContext().getRequestDispatcher("/Servlete?acao=listar").forward(request, response);
    }  
}